using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.Adapters;
using System.Data.SqlClient;

public partial class Register : System.Web.UI.Page
{
    private String RoleToJoin = "";
   
    private String[] states = { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", 
                                    "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", 
                                    "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", 
                                    "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", 
                                    "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WV", 
                                    "WI", "WY"};
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.IsInRole("Administrator"))
        {
            rbAdmin.Visible = true;
            rbInstructor.Visible = true;
            rbStudent.Visible = true;
            lblOffice.Visible = true;
            txtboxOffice.Visible = true;
            rfvOffice.Enabled = true;
        }
        else 
        {
            rbAdmin.Visible = false;
            rbInstructor.Visible = false;
            rbStudent.Visible = false;
            lblOffice.Visible = false;
            txtboxOffice.Visible = false;
            rfvOffice.Enabled = false;
        }
        for (int i = 0; i < states.Length; i++)
        {
            ddlStates.Items.Add(states[i].ToString());
        }
        ddlStates.SelectedIndex = 5;
    }
    
    protected void CreateUser_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        CreateUserWizard cu = (CreateUserWizard)sender;
        SqlConnection conn;
        SqlCommand comm;
        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;
        conn = new SqlConnection(connectionString);
        
        try
        {

            conn.Open();

            String firstName = txtboxFirstName.Text;
            String lastName = txtboxLastName.Text;
            String address = txtboxAddress.Text;
            String city = txtboxCity.Text;
            String state = ddlStates.Text;
            int zip = Convert.ToInt32(txtboxZip.Text.ToString());
            String ssn = txtboxSSN.Text;
            String phone = txtboxPhone.Text;
            String email = txtboxEmail.Text;
            String username = cu.UserName.Trim().ToUpper().ToString();
            String password = cu.Password.Trim().ToString();
            String status = "A";
            int office = 0;
            if (txtboxOffice.Visible == true)
            {
                office = Convert.ToInt32(txtboxOffice.Text.ToString());
            }

            string insertInstructor = "INSERT INTO Instructor " +
                "(first_Name, last_Name, address, city, state, zip, office_Num, phone_Num, email, username, password, status) " +
                "VALUES (@first, @last, @address, @city, @state, @zip, @office, @phone, @email, @user, @pass, @status)";
            string insertStudent = "INSERT INTO Student " +
                "(first_Name, last_Name, address, city, state, zip, ssn, phone_Num, email, username, password, status) " +
                "VALUES (@first, @last, @address, @city, @state, @zip, @ssn, @phone, @email, @user, @pass, @status)";
            string insertAdmin = "INSERT INTO Admin " +
                "(first_Name, last_Name, address, city, state, zip, ssn, phone_Num, email, username, password, status) " +
                "VALUES (@first, @last, @address, @city, @state, @zip, @ssn, @phone, @email, @user, @pass, @status)";


            if (User.IsInRole("Administrator") || User.IsInRole("Instructor"))
            {
                cu.LoginCreatedUser = false;
            }
            else
            {
                cu.LoginCreatedUser = true;
            }

            if (rbAdmin.Checked == true)
            {
                RoleToJoin = "Administrator";
                Roles.AddUserToRole(cu.UserName, RoleToJoin);
                comm = new SqlCommand(insertAdmin, conn);
            }
            else if (rbInstructor.Checked == true)
            {
                RoleToJoin = "Instructor";
                Roles.AddUserToRole(cu.UserName, RoleToJoin);
                comm = new SqlCommand(insertInstructor, conn);
            }
            else
            {
                RoleToJoin = "Student";
                Roles.AddUserToRole(cu.UserName, RoleToJoin);
                comm = new SqlCommand(insertStudent, conn);
            }

            comm.Parameters.AddWithValue("@first", firstName);
            comm.Parameters.AddWithValue("@last", lastName);
            comm.Parameters.AddWithValue("@address", address);
            comm.Parameters.AddWithValue("@city", city);
            comm.Parameters.AddWithValue("@state", state);
            comm.Parameters.AddWithValue("@zip", zip);
            comm.Parameters.AddWithValue("@ssn", ssn);
            comm.Parameters.AddWithValue("@phone", phone);
            comm.Parameters.AddWithValue("@email", email);
            comm.Parameters.AddWithValue("@user", username);
            comm.Parameters.AddWithValue("@pass", password);
            comm.Parameters.AddWithValue("@status", status);
                
            if (RoleToJoin == "Admin" || RoleToJoin == "Instructor")
            {
                comm.Parameters.AddWithValue("@office", office);
                comm.ExecuteNonQuery();
            }
            else
            {
                comm.ExecuteNonQuery();
            }
        }
        catch
        {
            e.Cancel = true;
        }
        finally
        {
            conn.Close();
        }
    }
   
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        txtboxFirstName.ReadOnly = true;
        txtboxLastName.ReadOnly = true;
        txtboxAddress.ReadOnly = true;
        txtboxCity.ReadOnly = true;
        txtboxEmail.ReadOnly = true;
        txtboxPhone.ReadOnly = true;
        txtboxSSN.ReadOnly = true;
        ddlStates.Enabled = false;
        txtboxZip.ReadOnly = true;
        btnContinue.Enabled = false;
        CreateUser.Visible = true;
        CreateUser.Email = txtboxEmail.Text;

    }




    protected void rbStudent_CheckedChanged(object sender, EventArgs e)
    {
        if (rbStudent.Checked == true)
        {
            rfvOffice.Enabled = false;
            txtboxOffice.Visible = false;
            lblOffice.Visible = false;
        }
        else
        {
            rfvOffice.Enabled = true;
            txtboxOffice.Visible = true;
            lblOffice.Visible = true;
        }
    }
}
